package com.tool.grab;

import android.content.Context;
import android.content.SharedPreferences;

import com.tool.grab.vpn.IPAddress;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;


public class Constant {

    public final static String VPN_NAME = "vpn.conf";
    public final static String PACKAGE_NAME = "PACKAGE_NAME";


    public final static String ROUTE_HOST = "0.0.0.0";
    public final static int ROUTE_PORT = 0;

    public final static String GOOGLE_DNS_FIRST = "8.8.8.8";
    public final static String GOOGLE_DNS_SECOND = "8.8.4.4";
    public final static String AMERICA = "208.67.222.222";
    public final static String CHINA_DNS_FIRST = "114.114.114.114";

    public final static int MUTE_SIZE = 2560;

    public final static IPAddress ADDRESS = new IPAddress("10.8.0.3",32);
    public final static int IP_INT = ipStringToInt(ADDRESS.Address);

    public static String timeToString(long time){
        DateFormat format = new SimpleDateFormat("yyyy:MM:dd_HH:mm:ss:s",Locale.getDefault());
        Date date = new Date(time);
        return format.format(date);
    }

    /**
     * 存入目标工程
     * @param context
     * @param packageName
     */
    public static void putPackageName(Context context,String packageName){
        SharedPreferences preferences = context.getSharedPreferences(VPN_NAME,Context.MODE_PRIVATE);
        preferences.edit().putString(PACKAGE_NAME,packageName).apply();
    }

    /**
     * 获取目标工程
     * @param context
     * @return
     */
    public static String getPackageName(Context context){
        SharedPreferences preferences = context.getSharedPreferences(VPN_NAME,Context.MODE_PRIVATE);
        return preferences.getString(PACKAGE_NAME,"video.like");
    }

    /**
     * IP字符串转int类型
     * @param ip
     * @return
     */
    public static int ipStringToInt(String ip) {
        String[] arrayStrings = ip.split("\\.");
        int r = (Integer.parseInt(arrayStrings[0]) << 24) | (Integer.parseInt(arrayStrings[1]) << 16) | (Integer.parseInt(arrayStrings[2]) << 8) | (Integer.parseInt(arrayStrings[3]));
        return r;
    }

}
